package com.jason.controller;

import com.jason.entities.JsonData;
import com.jason.entities.Payment;
import com.jason.service.PaymentService;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/v1/payment")
public class PaymentController {
    @Resource
    private PaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;
    @Resource
    private DiscoveryClient discoveryClient;
    @PostMapping("/inser")
    public JsonData InsertRecord(@RequestBody Payment payment){
        System.out.println(payment.getSerial());
        int i = paymentService.create(payment);

        if (i>0){
            System.out.println("OK");
            return JsonData.BuildSuccess(200,"插入成功，端口号："+serverPort,payment);
        }else {
            return JsonData.buildError("添加失败");
        }

    }
    @GetMapping("/list/{id}")
    public JsonData Select(@PathVariable Long id ){
        Payment paymentById = paymentService.getPaymentById(id);
        return JsonData.BuildSuccess(200,"插入成功，端口号："+serverPort,paymentById);
    }
    @GetMapping(value = "/discovery")
    public Object dicovery(){
        List<String> list = discoveryClient.getServices();
        return JsonData.BuildSuccess(0,"",list);

    }

}
